﻿using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using VOD.Models;
using VOD.ViewModels;

namespace VOD.Controllers
{
    public class MovieController : Controller
    {
        readonly VODEntities _db = new VODEntities();
        //
        // GET: /Movie/
        public ActionResult Index()
        {
            
            return View();
        }

        //显示电影详情
        // GET: /Movie/Details/5
        public ActionResult Details(int id)
        {
            var video = _db.Videos.Find(id);
            return View(video);
        }
        
        //频道首页栏目列表
        public ActionResult Column(string colTitle,string filter,int? type)
        {
            //筛选出所有电影频道的电影
            var videos = _db.Videos
                .Include(v => v.VideoLanguage)
                .Include(v => v.VideoAndTypes.Select(t => t.VideoType))
                .Include(t => t.PlayRecords)
                .Where(v => v.Recycled != 1 && v.CategoryID == 2)
                .OrderByDescending(v => v.UpdateDate)
                .ToList();
            //switch (filter.ToLower())
            //{
            //    case "hot"://热门
            //        videos = videos.OrderByDescending(v => v.PlayRecords.Count).ToList();
            //        break;
            //}
            if (type > 0)
            {
                //类别
                videos = videos.Where(v => v.VideoAndTypes.Any(t => t.TypeID == type)).ToList();
            }
            ViewBag.ColTitle = colTitle;
            videos = videos.Take(5).ToList();
            return View(videos);
        }

        // 电影频道首页的电影导航条
        public ActionResult TypeList()
        {
            var types = _db.VideoTypes.Where(t => t.CategoryID == 2).ToList();
            ViewBag.CategoryID = 2;
            return View(types);
        }

        // 年份列表
        public ActionResult YearList(int? categoryID)
        {
            var years = from v in _db.v_Videos
                        where v.ShowYear != null && v.CategoryID == 2
                        group v by v.ShowYear into g
                        select new ShowYear
                        {
                            Year = g.Key,
                        };
            ViewBag.CategoryID = categoryID;
            return View(years);
        }

        public ActionResult AreaList(int? categoryID)
        {
            var areas = from v in _db.v_Videos
                        where v.CategoryID == 2 && v.AreaName != null
                        group v by v.AreaName into g
                        select new AreaList
                        {
                            Name = g.Key
                        };
            ViewBag.CategoryID = 2;
            return View(areas);
        }

        //播放界面
        public ActionResult Play(int id)
        {
            var video = _db.v_Videos.Find(id);
            return View(video);
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}
